package errors

import (
	json "github.com/json-iterator/go"
)

// New returns an error that formats as the given code and message.
func NewJsonError(code int, message string) error {
	return &jsonError{Code: code, Message: message}
}

// jsonError is a implementation of error.
type jsonError struct {
	Code    int    `json:"code"`
	Message string `json:"message,omitempty"`
	*stack         `json:"-"`
}

// Error return the error string
func (e jsonError) Error() string {
	str, _ := json.MarshalToString(e)
	return str
}


func (e jsonError) StackTrace() StackTrace {
	return callers().StackTrace()
}

// Marshal adapts to json/encoding Marshal API
//
// Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
// Refer to https://godoc.org/encoding/json#Marshal for more information
func (e *jsonError) MarshalJSON() ([]byte, error) {
	return json.Marshal(e)
}

// Unmarshal adapts to json/encoding Unmarshal API
//
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
// Refer to https://godoc.org/encoding/json#Unmarshal for more information
func (e *jsonError) UnmarshalJSON(data []byte) error {
	return json.Unmarshal(data, e)
}
